<%@page import="Filesharing.FileModel"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List File</title>
<link rel="stylesheet" type="text/css" href="css/FinalProjectCSS.css"/>
<script type="text/javascript" src="table.js"></script>
<script type="text/javascript" src="js/MasterJS.js"></script>
<script type="text/javascript">
	function flyToPage(task) {
		document.forms[0].task.value = task;
		document.forms[0].submit();
	} 
	
	function downloadFile(filePathDl) {
		//alert("donlod " + filePathDl);
		document.forms[0].task.value = "downloadFile";
		document.forms[0].elements['modelFile.filePath'].value = filePathDl;
		document.forms[0].submit();
	
	}
	
	function updateFile(fileIdUpd, uploaderIdUpd) {
		document.forms[0].task.value = "updateFile";
		document.forms[0].elements['modelFile.fileId'].value = fileIdUpd;
		//alert("fileId: " + document.forms[0].elements['modelFile.fileId'].value);
		document.forms[0].elements['modelFile.uploaderId'].value = uploaderIdUpd;
		document.forms[0].submit();
	}
	 
	function deleteFile(fileIdDel, fileNameDel, uploaderIdDel) {
		 r = confirm("Delete " +fileNameDel+ " file ?");
		 if(r == true) {
			 document.forms[0].task.value = "deleteFile";
			 document.forms[0].elements['modelFile.fileId'].value = fileIdDel;
			 document.forms[0].elements['modelFile.fileName'].value = fileNameDel;
			 document.forms[0].elements['modelFile.uploaderId'].value = uploaderIdDel;
			 document.forms[0].submit();	
		 }
	} 
	
	function openFile(path) {
		document.forms[0].task.value = "openFile";
		document.forms[0].pathOpenFile.value = path;
		//alert(path);
		document.forms[0].submit();
	}
	
	function searchByParam(selectedParam) {
		document.forms[0].elements['param_fileName'].value = document.getElementById('paramFileName').value;
		document.forms[0].elements['param_uploaderId'].value = document.getElementById('paramUploaderId').value;
		document.forms[0].elements['param_description'].value = document.getElementById('paramDesc').value;
		document.forms[0].elements['param_dtmUpload'].value = document.getElementById('paramDtmUpload').value;
		document.forms[0].elements['param_dtmupd'].value = document.getElementById('paramDtmupd').value;
		document.forms[0].elements['selectedParam'].value = selectedParam;
		document.forms[0].task.value = "listFile";
		document.forms[0].submit();
	}
	
	function setDisplay() {
		document.getElementById('paramFileName').value = document.forms[0].elements['param_fileName'].value;
		document.getElementById('paramDesc').value = document.forms[0].elements['param_description'].value; 
		document.getElementById('paramUploaderId').value = document.forms[0].elements['param_uploaderId'].value;
		document.getElementById('paramDtmUpload').value = document.forms[0].elements['param_dtmUpload'].value;
		document.getElementById('paramDtmupd').value = document.forms[0].elements['param_dtmupd'].value;
		var selectedParam = document.forms[0].elements['selectedParam'].value;
		if(selectedParam==null) selectedParam = 'paramFileName';
		else if(selectedParam == '') selectedParam = 'paramFileName';
		document.getElementById(selectedParam).focus();
	}
	
	function showFileNotFound(fileNameNotFound){
		alert("File " +fileNameNotFound+ " does not exist");
	}
	
</script>

<%
 
	if(session.getAttribute("result") != null)
	{
		String message = session.getAttribute("result").toString();
		%>
			<script type="text/javascript">
				window.alert('<%=message%>');
			</script>
		<%
			session.removeAttribute("result");
	}
%> 

</head>
<body onkeydown="javascript:disableF5(event, '');" onload="setDisplay();">
	<center>
		<table width="100%" class="bodyTable boxShadow">
		
			<!-- HEADER -->
			<tr>
				<td colspan="2"><%@include file="/include/Header.jsp"%></td>
			</tr>
			
			<!-- CONTAINER -->
			<tr>
				<td rowspan="2" width="200px" class="navigationBox"><%@include file="/include/Navigation.jsp"%></td>
				<td class="headerBox blueBackground">File Sharing</td>
			</tr>
			<tr>
				<td align="center" class="contentStyle formListStyle"><br>
					<html:form action="/Filesharing" method="post">
						<html:hidden property="task" name="fileShareForm" />
						<html:hidden property="modelFile.filePath" name="fileShareForm" />
						<html:hidden property="modelFile.fileId" name="fileShareForm" />
						<html:hidden property="modelFile.fileName" name="fileShareForm" />
						<html:hidden property="modelFile.uploaderId" name="fileShareForm" />
						<html:hidden property="pathOpenFile" name="fileShareForm"/>
						
						<html:hidden property="param_fileName" name="fileShareForm" />
						<html:hidden property="param_uploaderId" name="fileShareForm" />
						<html:hidden property="param_description" name="fileShareForm" />
						<html:hidden property="param_dtmUpload" name="fileShareForm" />
						<html:hidden property="param_dtmupd" name="fileShareForm" />
						<html:hidden property="selectedParam" name="fileShareForm" />
						
						<table align="center" width="95%" border="0" class="boxShadow example table-autosort table-autopage:10 table-stripeclass:alternate table-page-number:t1page table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount" id="t1">
							<thead>
								<tr class="headerBox formHeaderBackground">
									<th colspan="2" class="formListStyle filterable table-sortable:default">File Name</th>
									<th class="formListStyle filterable table-sortable:default">Description</th>
									<th class="formListStyle filterable table-sortable:default">Uploader ID</th>
									<th class="formListStyle filterable table-sortable:default">Upload</th>
									<th class="formListStyle filterable table-sortable:default">Last Update</th>
									<th align="center" style="vertical-align: middle;"></th>
								</tr>
								<tr class="headerBox">
									<th colspan="2" align="center" style="padding:5px 7px;">
										<!-- <input name="filter" size="8" onkeyup="Table.filter(this,this)"/> -->
										<input type="text" id="paramFileName" style="width: 250px;" onchange="javascript:searchByParam('paramFileName');" />
									</th>
									<th align="center" style="padding:5px 7px;">
										<input type="text" id="paramDesc" style="width: 250px;" onchange="javascript:searchByParam('paramDesc');"/>
									</th>
									<th align="center" style="padding:5px 7px;">
										<input type="text" id="paramUploaderId" size="8" onchange="javascript:searchByParam('paramUploaderId');"/>
									</th>
									<th align="center" style="padding:5px 7px;">
										<input type="text" id="paramDtmUpload" size="8" onchange="javascript:searchByParam('paramDtmUpload');"/>
									</th>
									<th align="center" style="padding:5px 7px;">
										<input type="text" id="paramDtmupd" size="8" onchange="javascript:searchByParam('paramDtmupd');"/>
									</th>
									<th align="center" style="vertical-align: middle;"></th>								
								</tr>
							</thead>
							<%  
								ArrayList listFile = (ArrayList)request.getAttribute("listFile");
								/* if(listFile == null) System.out.println("List File NULL!!"); */
								int i = 0;
							%>
							<tbody>
								<logic:empty name="fileShareForm" property="listFile">
									<tr>
										<td colspan="7" align="center">
											<center>Data Empty</center>
										</td>
									</tr>
								</logic:empty>
								
								<logic:notEmpty name="fileShareForm" property="listFile">
									<logic:iterate id="listData" name="fileShareForm" property="listFile">
									
									<%
									
										FileModel tmpListFile = (FileModel)listFile.get(i);
										String tmpFileName = tmpListFile.getFileName();
										boolean cekFileName = false;
										if (tmpFileName.contains(".pptx") || tmpFileName.contains(".docx") || tmpFileName.contains(".xlsx") || 
												tmpFileName.contains(".ppt") || tmpFileName.contains(".doc") || tmpFileName.contains(".xls") || 
												tmpFileName.contains(".pdf")) {
											cekFileName = true;
										}
									%>
										<tr class="formListBackground">
											<%-- <td><bean:write name="listData" property="fileId"></bean:write></td> --%>
											<td colspan="2" align="center" class="formListStyle leftAlign" title="<bean:write name="listData" property="fileName"></bean:write>">
												<bean:write name="listData" property="showFileName"></bean:write>
												
												<% if(cekFileName) {
													if(tmpListFile.getIsFileExist()) {%>
												
													<a href="javascript:void(0)" onclick="javascript:openFile('<%= tmpListFile.getFileName() %>')">
														<img src="images/fileviewer.png" width="17px" height="17px" />
													</a>
													
													<% } else { %>
														<a href="javascript:void(0)" onclick="javascript:showFileNotFound('<bean:write name="listData" property="fileName"></bean:write>');">
															<img src="images/fileviewer.png" width="17px" height="17px" />
														</a>
													<% } %>
												<% } %>
												
											</td>
											<td align="justify" class="formListStyle leftAlign" width="225px"><bean:write name="listData" property="description"></bean:write></td>
											<%-- <td><bean:write name="listData" property="filePath"></bean:write></td> --%>
											<td align="center" class="formListStyle leftAlign" width="50px"><bean:write name="listData" property="uploaderId"></bean:write></td>
											<td align="center" class="formListStyle leftAlign" width="50px"><bean:write name="listData" property="dtmUpload"></bean:write></td>
											<td align="center" class="formListStyle leftAlign" width="50px"><bean:write name="listData" property="dtmupd"></bean:write></td>
											<td align="center" class="formListStyle leftAlign" width="100px">
												<% if(tmpListFile.getIsFileExist()) {%>
													<a href="<bean:write name="listData" property="filePath"></bean:write><bean:write name="listData" property="fileName"></bean:write>" download>
														<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
													</a>&nbsp;
												<% } else { %>
													<a href="javascript:void(0);" onclick="javascript:showFileNotFound('<bean:write name="listData" property="fileName"></bean:write>');">
														<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
													</a>&nbsp;
												<% } %>
												<%	if("admin".equalsIgnoreCase(user.getRole()) || tmpListFile.getUploaderId().equalsIgnoreCase(user.getUserid())) { %>
													<img src="images/editButton.png" title="edit" style="cursor: pointer;" 
														onclick="javascript:updateFile('<bean:write name="listData" property="fileId" />', '<bean:write name="listData" property="uploaderId" />');"/>&nbsp;
													<img src="images/deleteButton.png" title="delete" style="cursor: pointer;" 
														onclick="javascript:deleteFile('<bean:write name="listData" property="fileId" />', '<bean:write name="listData" property="fileName" />', '<bean:write name="listData" property="uploaderId" />');"/>
												<% } i++; %> 
											</td>
										</tr>
									</logic:iterate>
								</logic:notEmpty>								
							</tbody>
							
							<tfoot>
								<tr class="headerBox formHeaderBackground">
									<td width="85px" class="table-page:previous formListStyle" style="cursor:pointer;">&lt; &lt; Previous</td>
									<td colspan="5" style="text-align:center; vertical-align: middle;">Page <span id="t1page"></span>&nbsp;of <span id="t1pages"></span></td>
									<td width="10%" align="right" class="table-page:next formListStyle" style="cursor:pointer;">Next &gt; &gt;</td>
								</tr>
								<tr>
									<td colspan="7" id="insertButton" title="add new" align="center" class="greyBackground">						
										<div class="insertButtonGridStyle" onclick="javascript:flyToPage('insertFile');">
											<table>
												<tr>
													<td class="formListStyle"><img src="images/addButton.png"></td>
													<td class="formListStyle"><b>Add New File</b></td>
												</tr>
											</table>
										</div>									
									</td>
								</tr>
								<tr class="headerBox formHeaderBackground">
									<td colspan="10" align="center" style="padding:5px 7px;"><span id="t1filtercount"></span>&nbsp;of <span id="t1allcount"></span>&nbsp;rows match filter(s)</td>
								</tr>
							</tfoot>
						</table>

					</html:form><br><br>
				</td>
			</tr>
			
			<!-- FOOTER -->
			<tr>
				<td align="center" colspan="2" class="headerBox blueBackground">&copy; OME.2013.All Rights Reserved.</td>
			</tr>
		</table>
	</center>
</body>
</html>